package com.whmnx.chitchat.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DynamicAdapter;
import com.whmnx.chitchat.base.BaseFragment;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.my.FabuActivity;
import com.whmnx.chitchat.ui.my.FabuFriendsActivity;
import com.whmnx.chitchat.ui.my.MyActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.KeyboardUtils;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.DynamicInputTextMsgDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2020/7/20 0020 18:16
 * version: 1.0
 *
 * @author Administrator
 */
public class MyFragment extends BaseFragment {
    @BindView(R.id.iv_user)
    CircleImageView ivUser;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.ib_right)
    ImageButton ibRight;
    @BindView(R.id.rl)
    RecyclerView rvDynamic;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private int page = 1;
    private int Rows = 10;
    private List<DynaminBean.ForumListBean> list;
    private ViewStub vsEmpty;
    private DynamicAdapter  mDynamicAdapter;
    private DynamicInputTextMsgDialog dynamicInputTextMsgDialog;
    private String forum_id,forumCommentID;
    private boolean islike = false;

    @Override
    protected int getLayoutResource() {
        return R.layout.my_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        refresh.setOnRefreshListener(refreshLayout -> getForumList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getForumList(false));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.FANBU)) {
                getForumList(true);
            } else if (messageEvent.getMessageKey().equals(EventConstants.FAMILY)) {
                ImageUtil.setImage(ivUser, SPUtil.getString(Constants.HEAD_IMG));
            }
        }
    }


    private void initView() {
        initInput();
        ImageUtil.setImage(ivUser, SPUtil.getString(Constants.HEAD_IMG));

        rvDynamic.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvDynamic.setItemAnimator(null);
        mDynamicAdapter = new DynamicAdapter(getActivity());
        rvDynamic.setAdapter(mDynamicAdapter);
        rvDynamic.setFocusableInTouchMode(false);
       // rvDynamic.setHasFixedSize(true);
        getForumList(true);

        KeyboardUtils.addKeyboardToggleListener(getActivity(), new KeyboardUtils.SoftKeyboardToggleListener() {
            @Override
            public void onToggleSoftKeyboard(boolean isVisible) {
                if (isVisible){

                }else {
                    if (dynamicInputTextMsgDialog != null){
                        dynamicInputTextMsgDialog.dismiss();
                    }
                }
            }
        });

        mDynamicAdapter.setReplyListener(new DynamicAdapter.ReplyListener() {
            @Override
            public void commentReplyUser(String item, String string,int position) {
                forum_id = item;
                forumCommentID = string;
                if (forumCommentID.equals("dian")){
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("ForumID", forum_id);
                    HttpClient.get(getActivity(), Constants.GETFORUMLIKES, params,new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean result) {
                            getForumList(true);
                        }
                    });
                }else {
                    dynamicInputTextMsgDialog.setShowType();
                }
            }
        });

      /*  mDynamicAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                startActivity(new Intent(getActivity(), FabuFriendsActivity.class).putExtra("data",mDynamicAdapter.getItem(i)));
            }
        });*/
    }

    private void initInput() {
        dynamicInputTextMsgDialog = new DynamicInputTextMsgDialog(getActivity());

        dynamicInputTextMsgDialog.setOnSendClickListener(new DynamicInputTextMsgDialog.OnSendClickListener() {
            @Override
            public void onSendClick(String text) {
                comment(text);
            }
        });
    }

    private void comment(String text) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("ForumID", forum_id);
        params.put("Content", text);
        params.put("ForumCommentID", forumCommentID);
        HttpClient.post2(getActivity(), Constants.POSTADDFORUMCOMMENT, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                getForumList(true);
            }
        });
    }


    private void getForumList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(getActivity(), Constants.GETFORUMLIST, params,new CallBack<DynaminBean>() {
            @Override
            public void onSuccess(DynaminBean result) {
                list = result.getForumList();
                if (isRefresh){
                    refresh.finishRefresh(true);
                    if (list.size() == 0){
                        showEmpty(true,vsEmpty,2);
                    }else {
                        showEmpty(false, vsEmpty,2);
                    }
                    mDynamicAdapter.replaceAll(list, true);
                }else {
                    if (list.size() > 0) {
                        refresh.finishLoadMore(true);
                        mDynamicAdapter.addAll(list);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

       /* mDynamicAdapter.setOnItemClickListener((viewHolder, view, i) ->
                startActivity(new Intent(getActivity(), DynamicDetailsActivity.class).putExtra("ForumID", mDynamicAdapter.getItem(i).getForum_ID())));*/
    }

    @OnClick({R.id.iv_user, R.id.ib_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_user:
                UItils.startActivity(MyActivity.class);
                break;
            case R.id.ib_right:
                UItils.startActivity(FabuActivity.class);
                break;
            default:
        }
    }
}
